// index.js
const app = getApp()
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

import {
    GetHomeList, //列表
    GetSchoolRankListUrl, // 院校排行榜
    GetCountdown, // 距离高考天数
    GetStudentgrades, // 查询我的成绩
    GetExpertreserveAdd, // 专家预约
} from '../../api/index'
import api from '../../config/api';

Page({
    data: {
        //tabbar
        tabbar: {},
        baseImgUrl:api.BaseImgUrl,
        featureList: [{
                image: 'index_menu1.png',
                title: '查院校',
                type: 'findschool'
            },
            {
                image: 'index_menu2.png',
                title: '查专业',
                type: 'findprofession'
            },
            {
                image: 'index_menu3.png',
                title: '查职业',
                type: 'findoccupation'
            },
            {
                image: 'index_menu4.png',
                title: '招生计划',
                type: 'enrollplan'
            },
            {
                image: 'index_menu5.png',
                title: '高考日程',
                type: 'enrollintro'
            },
            {
                image: 'index_menu6.png',
                title: '一分一段',
                type: 'scoreparagraph'
            },
            {
                image: 'index_menu7.png',
                title: '在线测评',
                type: 'evaluation'
            },
            {
                image: 'index_menu8.png',
                title: '大学排名',
                type: 'schoolrank'
            },
            {
                image: 'index_menu9.png',
                title: '批次线',
                type: 'batchline'
            },
            {
                image: 'index_menu10.png',
                title: '志愿单',
                type: 'volunteer'
            }
        ],
        // listArray: [],
        expertArray:[], // 报考专家数据
        messageArray:[], // 高考资讯数据
        serviceArray:[], // 服务介绍数据
        schoolAlumnusListArray: [], // 校友会排名
        schoolShulianListArray: [], // 武书连排名
        schoolKeusoftListArray: [], // 软科排名
        dataSourceArray: [{
                dictValue: "0",
                dictLabel: "高考资讯"
            },
            {
                dictValue: "1",
                dictLabel: "院校排行"
            },
            {
                dictValue: "2",
                dictLabel: "报考专家"
            },
            {
                dictValue: "3",
                dictLabel: "服务介绍"
            }
        ],
        type: "学校排名",
        imageBase64One:'http://47.92.28.18:5099/img/applet/index_select_bg1.png',
        imageBase64Two:'http://47.92.28.18:5099/img/applet/index_select_bg2.png',
        score: '',
        subject:'',
        show: false, // 预约 
        // tabSelectIndex: 0,
        pageNo: 1,
        totalPages: 0,
        nomore: false,
        changePage: false, // 切换页面是否完成
        days:'', // 高考天数
        year:'', // 哪一年高考
        nameString:'', // 预约专家姓名
        phoneString:'', // 预约专家手机号
        expertId:'', // 专家id
        lot:'', // 专科还是本科
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this._GetCountdown()
        this._GetSchoolRankListUrl("?pageNum=0&pageSize=3", 0, false)
        this._GetSchoolRankListUrl("?pageNum=0&pageSize=3", 1, false)
        this._GetSchoolRankListUrl("?pageNum=0&pageSize=3", 2, false)
        //  console.log(this.data.imageBase64One)
        this._GetHomeList("?pageNum=1&pageSize=10", 1, false)
        this._GetHomeList("?pageNum=1&pageSize=10", 2, false)
        this._GetHomeList("?pageNum=1&pageSize=10", 3, false)
        
    },


    // 选大学
    chooseCollegiateAction() {
        Toast('暂未开放')
        return
        wx.navigateTo({
          url: '/pages/index/chooseprofession/chooseprofession',
        })
    },

    // 选高中
    chooseHeightSchoolAction() {
        Toast('暂未开放')
        return
        wx.navigateTo({
            url: '/pages/index/choosehighschool/choosehighschool',
        })
    },

    // 分数输入事件
    scoreInputAction: function (event) {
        console.log(event)
        this.setData({
            score: event.detail.value
        })
    },

    // 预约
    aboutChildrenAction(event) {
        console.log(event)
        console.log('预约')
        this.setData({
            expertId:event.detail,
            show: true
        })
    },

    // 输入框输入事件
    inputEvent(event) {
        console.log(event)
        let id = event.target.id
        let value = event.detail.value
        if (id == 'name') {
            this.setData({
                nameString:value
            })
        } else {
            this.setData({
                phoneString:value
            })
        }
    },

    /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

},
onShareTimeline: function (){

},

    // 取消预约
    cancelShowAction() {
        this.setData({
            show: false
        })
    },

    // 确定预约
    makeShowAction() {
        console.log('确定预约')
        if (this.data.nameString == '') {
            Toast('请输入姓名')
            return
        }
        if (this.data.phoneString == '') {
            Toast('请输入手机号')
            return
        }
        this.setData({
            show: false
        })
        let openid = wx.getStorageSync('openId') || '';
        if (openid != '') {
            this._GetExpertreserveAdd({openId:openid,name:this.data.nameString, tel:this.data.phoneString, expertId:this.data.expertId}, true)
        } else {
            wx.navigateTo({
              url: '/pages/login/login',
            })
        }
    },

    // 开通vip
    openVipAction() {
        wx.navigateTo({
            url: '/pages/mine/openvip/openvip',
        })
    },

    // 输入分数
    selectScoreAction() {
        let token = wx.getStorageSync('token') || '';
        let openid = wx.getStorageSync('openId') || '';
        if (token == '' || openid == '') {
            wx.navigateTo({
              url: '/pages/login/login',
            })
            return
        }

        wx.navigateTo({
            url: '/pages/mine/editgrade/editgrade',
        })
    },

    // 智能填报
    inteligentFillingAction: function () {
        if (this.data.score == '') {
            Toast('请输入分数')
            return
        }
        wx.navigateTo({
            url: '/pages/index/intelligentfilling/intelligentfilling',
        })
    },

    // 功能按钮点击事件
    featureSelectAction: function (event) {
        console.log(event)
        let itemType = event.currentTarget.dataset.type
        let url = ''
        if (itemType == 'findschool') { // 查院校
            url = '/pages/index/findschool/findschool'
        } else if (itemType == 'schoolrank') { // 大学排名
            url = '/pages/index/schoolrank/schoolrank?selectindex=0'
        } else if (itemType == 'findprofession') { // 查专业
            url = '/pages/index/checkprofession/checkprofession'
        } else if (itemType == 'batchline') { // 批次线
            url = '/pages/index/batchline/batchline'
        } else if (itemType == 'findoccupation') { // 查职业
            url = '/pages/index/checkoccupation/checkoccupation'
        } else if (itemType == 'scoreparagraph') { // 一分一段
            url = '/pages/index/oneRange/oneRange'
        } else if (itemType == 'enrollplan') { // 招生计划
            let token = wx.getStorageSync('token') || '';
            let openid = wx.getStorageSync('openId') || '';
            if (token == '' || openid == '') {
                wx.navigateTo({
                url: '/pages/login/login',
                })
                return
            }
            url = '/pages/index/enrollmentplan/enrollmentplan'
        }  else if (itemType == 'volunteer') { // 志愿单
            let token = wx.getStorageSync('token') || '';
        let openid = wx.getStorageSync('openId') || '';
        if (token == '' || openid == '') {
            wx.navigateTo({
              url: '/pages/login/login',
            })
            return
        }
            // url = '/pages/mine/mywish/mywish'
            // url = '/pages/index/currentdesire/currentdesire'
            url= '/pages/index/currentdesire/currentdesire?type=' + this.data.lot
        }  else if (itemType == 'enrollintro') { // 简章
            // url = '/pages/index/jianzhang/jianzhang'
            url = '/pages/index/examinationschedule/examinationschedule'
            // url = '/pages/index/examinationscheduleNew/examinationscheduleNew'
        }  else if (itemType == 'evaluation') { // 在线测评
            wx.switchTab({
              url: '/pages/evaluation/evaluation',
            })
            return
        } 


        wx.navigateTo({
            url: url,
        })
    },

    // 选科点击事件
    subjectAction(event) {
        let token = wx.getStorageSync('token') || '';
        let openid = wx.getStorageSync('openId') || '';
        if (token == '' || openid == '') {
            wx.navigateTo({
              url: '/pages/login/login',
            })
            return
        }
        let index = event.currentTarget.dataset.index
        let url = ''
        if (index == '0') { // 按选科插专业
            url = '/pages/index/disciplinetosubject/disciplinetosubject'
        } else if (index == '1') { // 按大学查选科
            url = '/pages/index/subjectToSchool/subjectToSchool'
        } else if (index == '2') { // 按专业查选科
            url = '/pages/index/subjectToProfession/subjectToProfession'
        }
        wx.navigateTo({
            url: url,
        })
    },

    // tab切换事件
    onChange(event) {
        // let tabIndex = event.detail.name;
        // console.log(tabIndex)
        // this.setData({
        //     tabSelectIndex: tabIndex,
        // })
  
    },

    _GetHomeList(data, type, isLoading) {
        console.log(data)
        console.log(this.data.listArray)
        let that = this
        // if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
        //     console.log('对吗')
        //     this.setData({
        //         nomore: true,
        //         changePage: true
        //     })
        //     return;
        // }
        console.log('type == ' + type)
        GetHomeList(data, type, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                if (type == 1) { // 报考专家
                    this.setData({
                        expertArray:res.rows
                    })
                } else if (type == 2) { // 高考资讯
                    this.setData({
                        messageArray:res.rows
                    })
                } else if (type == 3) { // 服务介绍
                    this.setData({
                        serviceArray:res.rows
                    })
                }
                // that.setData({
                //     changePage: true,
                //     listArray: that.data.listArray.concat(array),
                //     pageNo: that.data.pageNo + 1,
                //     totalPages: res.total,
                // });

                // if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                //     that.setData({
                //         nomore: true
                //     })
                // }
            } else {
                Toast(res.msg)
            }
        })
    },


    _GetSchoolRankListUrl(data, type, isLoading) {
        console.log(data)
 
        GetSchoolRankListUrl(data, type, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                if (type == 0) {
                    this.setData({
                        schoolAlumnusListArray: array
                    });
                } else if (type == 1) {
                    this.setData({
                        schoolShulianListArray: array
                    });
                } else if (type == 2) {
                    this.setData({
                        schoolKeusoftListArray: array
                    });
                }
            } else {

                // Toast(res.msg)
            }
        })
    },


    _GetCountdown(data, isLoading) {
        console.log('距离高考天数')
        GetCountdown(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                this.setData({
                    days:res.data.days,
                    year:res.data.year,
                })
            } else {

                // Toast(res.msg)
            }
        })
    },

    // 专家预约
    _GetExpertreserveAdd(data, isLoading) {
        console.log('专家预约')
        GetExpertreserveAdd(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            
            if (code == 200) {
                Toast('预约成功')
            } else {
                Toast(res.msg)
            }
        })
    },
    
    


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // app.hideTabBar()
    },

    onHidePopup: function () {
        this.setData({
            show: false
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // if (this.data.changePage == false) {
        //     return
        // }
        // if (this.data.tabSelectIndex == 0) {
        //     return
        // }
        // console.log('上拉加载更多')
        // this._GetHomeList("?pageNum=" + this.data.pageNo + '&pageSize=10', this.data.tabSelectIndex, false)
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let openid = wx.getStorageSync('openId') || '';
        if (openid != '') {
            this._GetStudentgrades('?openId=' + openid, false)
        } else {
            this.setData({
                score:'',
                subject:''
            })
        }
    },


    // 我的成绩查询
    _GetStudentgrades(data, isLoading) {
        GetStudentgrades(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                let mutableArray = [];
                if (array.length != 0) {
                    let model = array[0]
                    if (model.isWuli == 1) {
                        mutableArray.push('物理')
                    } else {
                        mutableArray.push('历史')
                    }
                    if (model.isHuaxue == 1) {
                        mutableArray.push('化学')
                    }
                    if (model.isShengwu == 1) {
                        mutableArray.push('生物')
    
                    }
                    if (model.isZhengzhi == 1) {
                        mutableArray.push('政治')
                    }
                    if (model.isDili == 1) {
                        mutableArray.push('地理')
                    }
                    let str = mutableArray.join('/'); // 使用逗号分隔
                    this.setData({
                        score:model.myScore,
                        subject:str,
                        lot:model.reserve1,
                    })
                }
            } else {
                // Toast(res.msg)
            }
        })
    },


})